<?php
namespace Home\Org\Util;
class PDO_DBLIB_MSSQL
{

    private $db;

    public function __construct(){

        $this->hostname = C('SQLSERVER_HOST');
        $this->port = C('SQLSERVER_PORT');
        $this->dbname = C('SQLSERVER_DATABASE');
        $this->username = C('SQLSERVER_USERNAME');
        $this->pwd = C('SQLSERVER_PASSWORD');

        $this->connect();

    }

    function query($sqlquery){
        $stmt = $this->db->prepare($sqlquery);
        $stmt->execute();
        $data = $stmt->fetchAll();
        return  $data;
    }

    function commitToSQLServer($sql){

        $sql = mb_convert_encoding($sql, "GBK", "UTF-8");

        try {
            $this->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
            $this->db->beginTransaction();
            $stmt = $this->db->prepare($sql);
            $r = $stmt->execute();
            $this->db->commit();
        }catch (PDOException $ex){
            $this->db->rollBack();
        }

        return $stmt;

    }

    public function close(){
        $this->db = null;
    }

    public function connect(){

        try {
            $this->db = new \PDO ("dblib:host=$this->hostname:$this->port;dbname=$this->dbname", "$this->username", "$this->pwd");

        } catch (PDOException $e) {
            $this->logsys .= "Failed to get DB handle: " . $e->getMessage() . "\n";
        }

    }

}